/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Ghe;
import DTO.Tau;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author PC
 */
public class TauDAO {
     public static boolean Them(Tau n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO tau(id, ten_tau, hang_sx, ngay_van_hanh, ngay_tao, loai_tau, ghi_chu) "
                    + "VALUES('%s','%s','%s','%s','%s','%d','%s')"
                    ,n.Id,n.TenTau,n.HangSanXuat,n.NgayVanHanh,n.NgayTao,n.LoaiTau,n.GhiChu);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from tau where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select t.id, t.ten_tau, t.hang_sx, t.ngay_van_hanh, t.ngay_tao, lt.ten_loai_tau, t.ghi_chu"
                    + "  from tau t, "
                    + "loai_tau lt where t.loai_tau=lt.id";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaNV)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From tau Where MaNV like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaNV);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
      public static Tau Get(String matau)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = String.format("Select * From Tau Where id = '%s'",matau);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            
            Tau t = new Tau();
            while(rs.next())
            {
               t.Id = rs.getString("id");
               t.LoaiTau = rs.getInt("loai_tau");
            }
            return t;
        }
        catch (Exception ex)
        {
        }
        return null;
    }
      
       public static boolean Update(Tau NV,String id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update tau set ten_tau = ?, hang_sx = ?, ngay_van_hanh = ? , ngay_tao= ?, loai_tau=? ,ghi_chu=?,id = ? Where id = ?";           
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, NV.TenTau);
            ps.setString(2, NV.HangSanXuat);
            ps.setString(3, NV.NgayVanHanh);
            ps.setString(4,NV.NgayTao);
            ps.setInt(5, NV.LoaiTau);
            ps.setString(6, NV.GhiChu);
            ps.setString(7, NV.Id);
            ps.setString(8,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
            
        }
        return false;
    }
}
